import 'package:favourcate/core/model/category_model.dart';
import 'package:favourcate/core/model/meal_model.dart';
import 'package:favourcate/core/viewmodel/meal_view_model.dart';
import 'package:favourcate/ui/widgets/meal_item.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:collection/collection.dart';

class GLMealContent extends StatelessWidget {
  const GLMealContent({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 获取路由参数
    final category = ModalRoute.of(context)!.settings.arguments as GLCategoryModel;
    
    return Selector<MealViewModel, List<GLMealModel>>(
      selector: (ctx, mealVm) {
        return mealVm.meals.where((meal) => meal.categories.contains(category.id)).toList();
      },
      // prev next ==> List<GLMealModel>
      shouldRebuild: (prev, next) => !const ListEquality().equals(prev, next),
      builder: (ctx, meals, child) {
        return ListView.builder(
          itemCount: meals.length,
          itemBuilder: (ctx, index) {
            return GLMealItem(meals[index]);
          }
        );
      },

    );
  }
}

/**
 
class GLMealContent extends StatelessWidget {
  const GLMealContent({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 获取路由参数
    final category = ModalRoute.of(context)!.settings.arguments as GLCategoryModel;
    
    return Consumer<MealViewModel>(
      builder: (ctx, mealVm, child) {
        // where === filter
        final meals = mealVm.meals.where((meal) => meal.categories.contains(category.id)).toList();
        return ListView.builder(
          itemCount: meals.length,
          itemBuilder: (ctx, index) {
            return ListTile(
              title: Text(meals[index].title),
            );
          }
        );
      }
    );
  }
}
 */